package com.dhcc.DRGsHosp.facade.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DisGroupAnalysisApplication;
import com.dhcc.DRGsHosp.facade.DisGroupAnalysisFacade;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.facade.dto.DrgsAnalysisDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.DrgsAnalysisAssembler;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;

/**
 * 
 * @title		:DisGroupAnalysisFacadeImpl.java
 * @description	:
 * @author		:mazhaoyan
 * @date		:2015年12月12日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014
 */
@Named("drgsHospFacadeImpl11")
public class DisGroupAnalysisFacadeImpl implements DisGroupAnalysisFacade{
	@Autowired
	private DisGroupAnalysisApplication disGroupAnalysisApplication;
	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade;
	/**
	 * 
	 * @methodname    :twoYearsCompare
	 * @description   : 两年比较
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2015年12月14日
	 */
	@Override
	public InvokeResult twoYearsCompare(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo( dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom, dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		Map<String,Object> result = disGroupAnalysisApplication.twoYearsCompare(DrgsAnalysisAssembler.toEntity(dto));
		return InvokeResult.success(result);
	}
	/**
	 * 
	 * @methodname    :drgsStructure
	 * @description   : 病组结构分析
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2015年12月14日
	 */
	@Override
	public InvokeResult drgsStructure(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo( dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom, dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		Map<String,Object> result = disGroupAnalysisApplication.drgsStructure(DrgsAnalysisAssembler.toEntity(dto));
		return InvokeResult.success(result);
	}
	/**
	 * 
	 * @methodname    :drgsDetail
	 * @description   : 病组结构详情
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2015年12月14日
	 */
	@Override
	public InvokeResult drgsDetail(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo( dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom, dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		String queryType=dto.getQueryType();
		List<Map<String,Object>> result = disGroupAnalysisApplication.drgsDetail(DrgsAnalysisAssembler.toEntity(dto));
		List<Map<String,Object>> resultData = new LinkedList<Map<String,Object>>();
		final String flag = queryType;
		
		Collections.sort(result, new Comparator<Map<String,Object>>(){
			public int compare(Map<String,Object> o1,Map<String,Object> o2){
				String s = "";
				if(flag.contains("this")){
					s = "TPCNT";
				}else{
					s="LPCNT";
				}
				
				BigDecimal v1 = (BigDecimal)o1.get(s);
				BigDecimal v2 = (BigDecimal)o2.get(s);
				int value1 = (v1==null ? 0:v1.intValue());
				int value2 = (v2==null ? 0:v2.intValue());
				return value2-value1;
			}
		});
		for(int i=0;i<result.size();i++){
			Map<String,Object> map = result.get(i);
			String key = "";
			if(flag.contains("this")){
				key = "THISORDER";
			}else{
				key="LASTORDER";
			}
			map.put(key, i+1);
			resultData.add(map);
		}
		return InvokeResult.success(resultData);
	}
	
	/**
	 * 
	 * @methodname    :overproofDrgsDetail
	 * @description   : 费用超标DRGs病组详情
	 * @param         :
	 * @author        :fulianxia
	 * @return        :
	 * @date          :2016年3月25日
	 */
	@Override
	public InvokeResult overproofDrgsDetail(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo(dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom,dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		List<Map<String,Object>> result = disGroupAnalysisApplication.overproofDrgsDetail(DrgsAnalysisAssembler.toEntity(dto));
		return InvokeResult.success(result);
	}
	
	/**
	 * 
	 * @methodname    :loadCase
	 * @description   : 病例数
	 * @param         :
	 * @author        :fulianxia
	 * @return        :
	 * @date          :2016年11月28日
	 */
	@Override
	public InvokeResult loadCase(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo(dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom,dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		List<Map<String,Object>> listData = disGroupAnalysisApplication.loadCase(DrgsAnalysisAssembler.toEntity(dto));
		return InvokeResult.success(listData);
	}
	/**
	 *  根据drgCode和年份查询主诊组信息
	 *  @Author QiaoYiLong
	 *  @param drgsAnalysis
	 *  @return 
	 *  @Date:2018年9月4日下午3:30:55
	 */
	@Override
	public InvokeResult getClinicGroupByDrgCode(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo(dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom,dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		//查询主诊组信息
		List<Map<String,Object>> listData = disGroupAnalysisApplication.getClinicGroupByDrgCode(DrgsAnalysisAssembler.toEntity(dto));
		return InvokeResult.success(listData);
	}
	
	/**
	 *  查询指定主诊组id的病例详情
	 *  @Author QiaoYiLong
	 *  @param drgsAnalysis (DrgsAnalysisDTO.drgCode必填，DrgsAnalysisDTO.monitorType存主诊组id)
	 *  @Date:2018年9月6日上午11:47:30
	 */
	@Override
	public InvokeResult loadClinicCase(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo(dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom,dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		List<Map<String,Object>> listData = disGroupAnalysisApplication.loadClinicCase(DrgsAnalysisAssembler.toEntity(dto));
		return InvokeResult.success(listData);
	}
}
